import gradio as gr
import requests
import json
import sys

def fun(prompt,max_tokens,repetition_penalty,presence_penalty,frequency_penalty,temperature,top_k,top_p):

  url = 'http://171.60.1.106:1025/generate'

  json_data={
  "prompt":prompt,
  "max_tokens":int(max_tokens),
  "repetition_penalty":repetition_penalty,
  "presence_penalty":presence_penalty,
  "frequency_penalty":frequency_penalty,
  "temperature":temperature,
  "top_k":int(top_k),
  "top_p":top_p,
  "stream":False
  }
  # 发送POST请求
  response = requests.post(url, json=json_data)
  context = response.text
  context=json.loads(context)
  context=context["text"][0]
  #print("\n\n")
  sys.stdout.write(context + '\n')
  return context


interface = gr.Interface(
    fn=fun,
    inputs=[gr.Textbox(label="请输入描述"),
                gr.Number(label="max_tokens",value=512),
                        gr.Number(label="repetition_penalty",value=1.03),
                        gr.Number(label="presence_penalty",value=1.2),
                        gr.Number(label="frequency_penalty",value=1.2),
                        gr.Number(label="temperature",value=0.5),
                        gr.Number(label="top_k",value=10),
                        gr.Number(label="top_p",value=0.95),


               ],
    outputs=gr.Textbox(label="结果")
)

# 将IP地址修改为0.0.0.0，这样可以让局域网内的其他设备访问此应用
interface.launch(server_name="171.60.1.106")

